<?php

/**
 * RabbitCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rabbitcms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: RoleController.php 3971 2010-07-25 10:26:42Z huuphuoc $
 * @since		2.0.0
 */
class Core_RoleController extends Zend_Controller_Action {
    /* ========== Backend actions =========================================== */

    /**
     * Add new role
     *
     * @return void
     */
    public function addAction() {
        $this->_helper->getHelper('viewRenderer')->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();

        $request = $this->getRequest();
        if ($request->isPost()) {
            $params = array();
            $params['name'] = $request->getPost('name');
            $params['description'] = $request->getPost('description');
            $params['lock'] = $request->getPost('lock', 0);

            $roleModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getRoleModel();
            $id = $roleModel->add($params);
            if ($id) {
                $this->_helper->getHelper('FlashMessenger')
                        ->addMessage('success:' . $this->view->translate('role_add_success'));
            } else {
                $this->_helper->getHelper('FlashMessenger')
                        ->addMessage('error:' . $this->view->translate('role_add_error'));
            }
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'core_role_list'));
        }
    }

    /**
     * Delete role
     *
     * @return void
     */
    public function deleteAction() {
        $this->_helper->getHelper('viewRenderer')->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();

        $request = $this->getRequest();
        if ($request->isPost()) {
            $id = $request->getPost('id');
            $roleModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getRoleModel();

            /**
             * Delete role if there's no users belonging to this role
             */
            $numUsers = $roleModel->countUsers($id);
            if ($numUsers == 0) {
                $roleModel->delete($id);
                $this->getResponse()->setBody('RESULT_OK');
            } else {
                $this->getResponse()->setBody('RESULT_ERROR');
            }
        }
    }

    /**
     * List roles
     *
     * @return void
     */
    public function listAction() {
        $roleModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getRoleModel();
        $roles = $roleModel->getRolesIncludeUser();
        $this->view->assign('roles', $roles);
    }

    /**
     * Lock role
     *
     * @return void
     */
    public function lockAction() {
        $this->_helper->getHelper('viewRenderer')->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();

        $request = $this->getRequest();
        if ($request->isPost()) {
            $id = $request->getPost('id');
            $lock = $request->getPost('lock');
            $roleModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getRoleModel();
            if ($roleModel->toggleLock($id)) {
                $this->getResponse()->setBody('RESULT_OK');
            } else {
                $this->getResponse()->setBody('RESULT_ERROR');
            }
            //else
            //$this->getResponse()->setBody('RESULT_ERROR');
        }
    }

}
